<?php
namespace app\index\controller\wanlshop;

use app\common\controller\Wanlshop;
use addons\wanlshop\library\WanlChat\WanlChat;
use think\Config;
use think\Db;

/**
 * 二维码
 * @internal
 */
class Getcode extends Wanlshop
{
    protected $noNeedLogin = '';
    protected $noNeedRight = '*';
    
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $shop=\app\index\model\wanlshop\Shop::get($this->shop->id);
        $code="";
        if(!empty($shop)){
            if(empty($shop["code"])){
                $code=$this->getCode($this->shop->id);
                $shop->code=$code;
                $shop->save();
            }else{
                $code=$shop["code"];
            }
        }
        $this->assign("code",$code);
        return $this->view->fetch();
    }
    /**
     * 生成二维码
     * */
    protected function getCode($id)
    {

        /*生成二维码 start*/
        $config = get_addon_config('qrcode');
        $qrCode = \addons\qrcode\library\Service::qrcode(["text"=>"/pages/home/dietOrder?id=".$id]);
        // 写入到文件
        $qrcodePath = ROOT_PATH . 'public/uploads/qrcode/';
        if (!is_dir($qrcodePath)) {
            @mkdir($qrcodePath);
        }
        if (is_really_writable($qrcodePath)) {
            $filePath = $qrcodePath  .md5("shop_id".$id). '.' . $config['format'];
            $qrCode->writeFile($filePath);
        }
        $url=cdnurl("/uploads/qrcode/".md5("shop_id".$id). '.' . $config['format']);
        /*生成二维码 end*/
        return $url;
    }
}
